package main

import (
	"fmt"
	"strings"
)

func main() {
	//practiceMap()

	//practicePointer()

	//practiceStruct()

	//practiceStruct1()

	dispatchCoin()

}

func dispatchCoin() {
	/*
		你有50枚金币，需要分配给以下几个人：Matthew,Sarah,Augustus,Heidi,Emilie,Peter,Giana,Adriano,Aaron,Elizabeth。
		分配规则如下：
		a. 名字中每包含1个'e'或'E'分1枚金币
		b. 名字中每包含1个'i'或'I'分2枚金币
		c. 名字中每包含1个'o'或'O'分3枚金币
		d: 名字中每包含1个'u'或'U'分4枚金币
		写一个程序，计算每个用户分到多少金币，以及最后剩余多少金币？
		程序结构如下，请实现 ‘dispatchCoin’ 函数
	*/
	var (
		coins = 50
		users = []string{
			"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
		}
		distribution = make(map[string]int, len(users))
	)
	for _, user := range users {
		if coins == 0 {
			break
		}
		if _, key := distribution[user]; !key {
			distribution[user] = 0
		}
		for _, i := range user {
			switch i {
			case 'e', 'E':
				if coins >= 1 {
					coins -= 1
					distribution[user]++
				}

			case 'i', 'I':
				if coins >= 2 {
					coins -= 2
					distribution[user] += 2
				}

			case 'o', 'O':
				if coins >= 3 {
					coins -= 3
					distribution[user] += 3
				}

			case 'u', 'U':
				if coins >= 1 {
					coins -= 4
					distribution[user] += 4
				}
			}
		}
	}
	fmt.Println(coins)
	fmt.Println(distribution)
}

type student struct {
	id   int
	name string
	age  int
}

func struct1(ce []student) {
	ce[1].age = 999
}

func practiceStruct1() {
	var ce []student //定义一个切片类型的结构体
	ce = []student{
		student{1, "xiaoming", 22},
		student{2, "xiaozhang", 33},
	}
	fmt.Println(ce) // [{1 xiaoming 22} {2 xiaozhang 33}]
	struct1(ce)
	fmt.Println(ce) // [{1 xiaoming 22} {2 xiaozhang 999}]
}

func practiceStruct() {
	type student struct {
		name string
		age  uint8
	}
	var classes map[string]*student = make(map[string]*student, 5)
	var students []student = []student{
		{name: "海阔天空", age: 20},
		{name: "逆天而行", age: 19},
		{name: "随风起舞", age: 18},
	}
	for _, stu := range students {
		classes[stu.name] = &stu
		fmt.Println(stu)
		fmt.Println(&stu)
	}
	for name, studentInfo := range classes {
		fmt.Printf("name:%s---%#v \n", name, studentInfo.name)
	}
}

func practicePointer() {
	//程序定义一个int变量num的地址并打印
	//将num的地址赋给指针ptr，并通过ptr去修改num的值
	var num int8 = 1
	var numPtr = &num
	fmt.Println(num)
	*numPtr = 2
	fmt.Println(num)
}

func practiceMap() {
	//写一个程序，统计一个字符串中每个单词出现的次数。比如：”how do you do”中how=1 do=2 you=1。
	//观察下面代码，写出最终的打印结果。
	var str string = "how do you do"
	var strArr []string = strings.Split(str, " ")
	var strMap map[string]int8 = make(map[string]int8, len(strArr))
	for _, strItem := range strArr {
		if _, key := strMap[strItem]; key {
			strMap[strItem]++
		} else {
			strMap[strItem] = 1
		}
	}
	fmt.Println(strMap) // map[do:2 how:1 you:1]
}
